import { type } from './typeConst';
import moment from 'moment'
import { nanoid } from 'nanoid';
// 每一条data {type:'pay',detailType:'eat',year:2022,month:202208,,day:20220810,money:15,id:'adkck234',remark:'备注'}
export default class db {
    static test(callback = null) {
        console.log('test');
        callback && callback()
    }

    // 所有数据
    static qryAllData(callback) {
        if (localStorage.getItem('allData')) {
            let allData = JSON.parse(localStorage.getItem('allData'))
            callback(allData)
        } else {
            let allData = []
            localStorage.setItem('allData', JSON.stringify(allData))
            callback(allData)
        }
    }

    //某个月的所有数据
    static qryAllDataByMonth(month, callback = null) {
        this.qryAllData(ret => {
            let rs = ret.filter((item, index) => {
                return item.month == month
            })
            callback && callback(rs)
        })
    }

    //某个月的所有支出
    static qryAllPayByMonth(month, callback = null) {
        this.qryAllData(ret => {
            let rs = ret.filter((item, index) => {
                return item.month == month && item.type == type.pay
            })
            callback && callback(rs)
        })
    }

    //某个月的所有收入
    static qryAllIncomeByMonth(month, callback = null) {
        this.qryAllData(ret => {
            let rs = ret.filter((item, index) => {
                return item.month == month && item.type == type.income
            })
            callback && callback(rs)
        })
    }

    //本月所有数据
    static qryCurrMonthData(callback = null) {
        let year = moment().year();
        let month = moment().month() + 1;
        if (month < 10) {
            month = '0' + month
        }
        let currMonth = year + '' + month
        this.qryAllData(ret => {
            let rs = ret.filter((item, index) => {
                return item.month == currMonth;
            })
            callback && callback(rs)
        })
    }

    static insertData(params, callback = null) {
        this.qryAllData(ret => {
            params.id = nanoid();
            ret.unshift(this.fixTime(params));
            localStorage.setItem('allData', JSON.stringify(ret))
            callback && callback()
        })
    }

    static delData(id, callback = null) {
        this.qryAllData(ret => {
            let rs = ret.filter((item, index) => {
                return item.id != id
            })
            localStorage.setItem('allData', JSON.stringify(rs))
            callback && callback(rs)
        })
    }

    static updateData(params, callback = null) {
        // params {id:'xxx',money:'xxx',type:'xx',typeDetail:'xx'}
        // money,type,typeDetail, 只会改这3个
        this.qryAllData(ret => {
            let target;
            let targetIndex;
            target = ret.filter((item, index) => {
                if (item.id == params.id) {
                    targetIndex = index

                }
                return item.id == params.id
            })
            ret[targetIndex] = { ...target, ...params }
            localStorage.setItem('allData', ret)
            callback && callback(ret)
        })
    }


    //清除 allData
    static clearAll(callback) {
        localStorage.clear();

        callback && callback()
    }

    // 
    static fixTime(params) {
        params.day = moment().format('YYYYMMDD');
        params.month = moment().format('YYYYMM');
        params.year = moment().format('YYYY');
        params.time = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        params.timeStamp = Date.now();
        return params;
    }
}